import Cookies from 'js-cookie'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  size: Cookies.get('size') || 'medium',
  //左侧菜单
  leftbar:{
    opened: Cookies.get('leftbarStatus') ? !!+Cookies.get('leftbarStatus') : true,
    withoutAnimation: false,
    showTitle:Cookies.get('leftbarShowTitle') ? !!Cookies.get('leftbarShowTitle') : false, //菜单上是否显示标题
    title:Cookies.get('leftbarTitle') ? Cookies.get('leftbarTitle') : '',  //菜单上显示的标题
    show:Cookies.get('leftbarShow') ? !!Cookies.get('leftbarShow') : false, //左侧菜单是否显示,
    parentPath:''
  }
}

const mutations = {
  SET_PARENT_PATH : (state,path) =>{
      Cookies.set('leftbarParentPath',path)
      state.leftbar.parentPath = path
  },
  SET_LEFTBAR_SHOW:(state,show) =>{
    Cookies.set('leftbarShow',show)
    state.leftbar.show = show
  },

  SET_LEFTBAR_TITLE:(state,title) => {
    Cookies.set('leftbarTitle',title)
    state.leftbar.title = title
  },
  SET_LEFTBAR_TITLE_SHOW:(state,show) =>{
    Cookies.set('leftbarShowTitle',show)
    state.leftbar.showTitle = show
  },


  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  },
  setParentPath({commit},path){
    commit('SET_PARENT_PATH',path)
  },
  setLeftbarShow({commit},show){
    commit('SET_LEFTBAR_SHOW',show)
  },
  setLeftbarTitle({commit},title){
    commit('SET_LEFTBAR_TITLE',title)
  },
  setLeftbarTitleShow({commit},titleShow){
    commit('SET_LEFTBAR_TITLE_SHOW',titleShow)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
